package com.lly.pro.common;

import com.xiaoleilu.hutool.json.JSONObject;

import java.util.*;
/**
 * 参数检查工具
 * @author Hommond
 * @date 2/22/2019
 */
public class ParamterCheckUtil {
    private static final Object[] LOGIN_PARAMS = {"userId","password","identity"};
    private static final Object[] CHANGE_PASSWORD_PARAMS = {"newPassword"};
    private static final Set LOGIN_PARAM_SET = new HashSet(Arrays.asList(LOGIN_PARAMS));
    private static final Set CHANGE_PASSWORD_PARAMS_SET  = new HashSet(Arrays.asList(CHANGE_PASSWORD_PARAMS));
    /**
     * 判断param是否带有参数集requiredParameters中的参数
     * @Param requiredParameters 要求应该有的参数集
     * @Param 前端传来的参数@RequestBody
     * @return
     */
    public static boolean checkParam(Set requiredParameters, JSONObject param){
        if (requiredParameters.equals(param.keySet())){
            return true;
        }
        else{
            return false;
        }
    }

    /**
     * 检查"/login"参数是否齐全
     * @Param param 前端传来的参数@RequestBody
     * @return
     */
    public static boolean isLoginParamComplete(JSONObject param){
        return checkParam(LOGIN_PARAM_SET,param);
    }

    /**
     * 检查修改密码功能参数是否齐全
     * @Param param 前端传来的参数@RequestBody
     * @return
     */
    public static boolean isChangePasswordParamComplete(JSONObject param){
        return checkParam(CHANGE_PASSWORD_PARAMS_SET,param);
    }

    /**
     * 检查参数是否符合要求
     * @Param clazz model或者entity
     * @Param param 前端传来的参数@RequestBody
     * @return
     */
    public static boolean isContainTargetParams(Class clazz,JSONObject param){
        return Tools.getFieldName(clazz).containsAll(param.keySet());
    }


}
